<?php
/**
 * @author baby&star
 *
 *
 */
class Singel extends sprder{
	//TODO - Insert your code here
	private $webRoot = NULL;
	
	function __construct($arr) {
		
		//TODO - Insert your code here
		
		parent::__construct($arr);
		
		$this->_url = preg_replace("/{\d+}/","",$this->_url);
		
		$domain = parse_url($this->_url);
		
		$this->host = $domain["scheme"]."//".$domain["host"];
		
		$this->webRoot = explode('/',$this->_url);
		
		array_pop($this->webRoot);
		
		$this->webRoot = implode('/',$this->webRoot);
		
	}
	function start(){
		$id = 1;
		while(true){
			
			//$data = "SearchCat1=-1&SearchCat14=-1&SearchCat2=-1&SearchCat3=-1&SearchCat8=1&SearchCat9=-1&lang=3&page=".$id;
			$data = $this->setPage.$id;
			
			$cts = $this->postWebContent($this->_url,$data);
			
			
			//$cts = '<TD VALIGN=TOP width=\'290\'><IMG SRC="images/big/EY5082_big.jpg" WIDTH=290 HEIGHT=297 ALT=""></TD>';
			//preg_match_all("/<img\s+border='0'\s+name=\"smallimage\"\s+src=\"([\d\D]*?)\"\s+width=\"\d+\"\s+height=\"\d+\">/",$cts,$rs);
			//$this->blockDelimiter = "/\'(http:\/\/.*?\.jpg)\'/";
			preg_match_all($this->blockDelimiter,$cts,$rs);
			
			if(empty($rs[1])){
				break;
			}
			
			//preg_match("/<TD\s+VALIGN=TOP\s+width='290'><IMG\s+SRC=\"([\d\D]*)\"\s+WIDTH=\d+\s+HEIGHT=\d+\s+ALT=\"\"><\/TD>/",$cts,$rs);
			
			
			//<img border='0' name="smallimage" src="images/small/EL5962_small.jpg" width="107" height="107">
			
			
			$img = $rs[1];
			
			foreach($img as $k=>$v){
				
				$this->saveFile($v);
				
			}
			
			$id++;
		}
	}
	/**
	 * 下载图片并且保存
	 * Enter description here ...
	 * @param unknown_type $img
	 */
	private function saveFile($img){
		$str = substr($img,0,1);
		if($str == "/"){
			$img = $this->host.'/'.$img;
		}else if(!preg_match("/http:\/\//",$img)){
			$dirLevel = explode('..',$$img);
			
			$len = count($dirLevel);
			
			$webUrl = $this->webRoot;
			
			if($len>2){
				
				$len--;
				
				$webUrl = explode('/',$this->webRoot);
				
				for($i=0;$i<$len;$i++){
					
					array_pop($webUrl);
					
					array_shift($dirLevel);
					
				}
				
				$webUrl = implode('/',$webUrl);
				
				$img = implode('',$dirLevel);
				
				$img = ltrim($img,'/');
				
			}else{
				
				$webUrl = explode('/',$this->webRoot);
				
				end($webUrl);
				
				$img = implode('/',$webUrl).'/'.$img;
			}
		}
		$img = str_replace("small","big",$img);
		//echo strlen($img);exit;
		if(@fopen($img,"r")&&strlen($img)<150){
			
			$filePath = $this->downFile($img);
			//保存
			$this->save($filePath);
		}else{
			echo "NO";
		}
	}
	//保存
	private function save($imgPath){
		if(empty($imgPath)){
			return false;
		}
		$db = new brand_products();
		$arr = array(
				'brandId'=>$this->_id,
				//'price'=>$price,
				//'detailHref'=>$detailHref,
				//'name'=>$name,
				'imgPath'=>$imgPath,
				'createDate'=>date('Y-m-d H:i:s'),
				'deleted'=>'0'
		);
		
		return $db->insertData($arr);
	}
	/**
	 *
	 */
	function __destruct() {

		//TODO - Insert your code here
	}
}
?>